package com.wc.AlgoOJ.LQ1036T;

import java.util.Scanner;


public class Answer2 {
    static int n, k, ans = 0;

    //检查是不是带有2 或 4
    static Boolean check(int parm) {
        while (parm > 0) {
            int t = parm % 10;
            if (t == 2 || t == 4) return true;
            parm /= 10;
        }
        return false;
    }

    public static void main(String[] args) {
//	f();
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        long[][][] dp = new long[2500 + 1][10 + 1][2500 + 1];//当前考虑的物品，当前选择的物品个数，当前选择的物品的重量，当前物品选还是没选
        while (t-- > 0) {
            n = scanner.nextInt();
            k = scanner.nextInt();
            int sum = 0;
            //从2500个物品里，选10个物品，且价值恰好为n

            dp[0][0][0] = 1;
            for (int i = 1; i <= n; i++) {
                dp[i][1][i] = 1;
                dp[i][0][0] = 1;
//			dp[1][0][0][0] = 1;
            }
            for (int i = 1; i <= n; i++) {//
                for (int j = 1; j <= k; j++) {//
                    for (int q = 1; q <= n; q++) {//
                        if (check(i)) {
                            dp[i][j][q] = dp[i - 1][j][q];
                        } else {
                            if (q >= i)
                                dp[i][j][q] = dp[i - 1][j - 1][q - i] + dp[i - 1][j][q];
                            else
                                dp[i][j][q] = dp[i - 1][j][q];
                        }
//					System.out.println(dp[i][j][q] + " " + i + " " + j + " " + q);
                    }
                }
            }

            long ans = 0;
            ans = dp[n][k][n];
            System.out.println(ans);
        }
    }

    private static void f() {
        //时间复杂度高了
        int dp[][][] = new int[k + 1][n + 1][n + 1];
        for (int i = 1; i <= n; i++) dp[1][i][i] = 1;
        for (int i = 1; i <= k; i++) {//10 遍历规模
            for (int j = 1; j <= n; j++) {//2500 遍历限制
                for (int q = 1; q <= j; q++) {//
                    if (check(q)) continue;
                    for (int p = q + 1; p <= j; p++) {
                        if (check(p)) continue;
                        dp[i][j][p] += dp[i - 1][j - p][q];
                    }
                }
            }
        }
    }
}